#include "listmodel.h"
#include "session.h"
#include "graph.h"

#include <QtGui/QtGui>

ListModel::ListModel(QObject* s, Graph g): QAbstractItemModel{s}, graph{g} {
}

int ListModel::columnCount(const QModelIndex& parent) const {
	return 2;
}
QVariant ListModel::data(const QModelIndex& index, int role) const {
	if(!index.isValid())
		return QVariant();
	switch(index.column()) {
		case 0:
			{
				Tree n=graph.trees()[index.row()];
				switch(role) {
					case Qt::DisplayRole:
						return index.row();
					case Qt::DecorationRole:
						return QIcon{n.completed() ? ":/images/breeze/dialog-ok.svg" : ":/images/breeze/emblem-important.svg"};
				}
			}
			break;
		case 1:
			{
				Tree n=graph.trees()[index.row()];
				switch(role) {
					case Qt::DisplayRole:
						return QString::fromStdString(n.name());
					case Qt::EditRole:
						return QString::fromStdString(n.name());
				}
			}
			break;
	}
	return QVariant();
}

Qt::ItemFlags ListModel::flags(const QModelIndex& index) const {
	if(!index.isValid())
		return 0;
	if(index.column()==1)
		return Qt::ItemIsEditable|QAbstractItemModel::flags(index);
	return QAbstractItemModel::flags(index);
}

bool ListModel::hasChildren(const QModelIndex& parent) const {
	if(!parent.isValid())
		return graph.trees().size()>0;
	return false;
}

QVariant ListModel::headerData(int section, Qt::Orientation orientation, int role) const {
	if(role!=Qt::DisplayRole)
		return QVariant();
	switch(section) {
		case 0:
			return QString("Index");
		case 1:
			return QString("Name");
	}
	return QVariant();
}

QModelIndex ListModel::index(int row, int column, const QModelIndex& parent) const {
	if(parent.isValid()) {
		return QModelIndex();
	} else {
		if(row>=0 && static_cast<size_t>(row)<graph.trees().size() && column<columnCount()) {
			return createIndex(row, column);
		} else {
			return QModelIndex();
		}
	}
}

QModelIndex ListModel::parent(const QModelIndex& index) const {
	return QModelIndex();
}

int ListModel::rowCount(const QModelIndex& parent) const {
	if(parent.column()>0)
		return 0;
	if(parent.isValid())
		return 0;
	return graph.trees().size();
}

bool ListModel::setData(const QModelIndex& index, const QVariant& value, int role) {
	/*
	if(role!=Qt::EditRole)
		return false;
	if(!index.isValid())
		return false;
	if(index.column()!=0)
		return false;
	tracer->renameNeuron(index.row(), value.toString());
	*/
	return false;
}

